#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "ComDriver.h"
#include "Timer.h"
#include "BSP.h"
#include "Protocol.h"
#include "ModbusRTU.h"
#include "Settings.h"
#include "CompressorControl.h"
#include "Register.h"
#include "ModbusRegister.h"
#include "led.h"
#include "24cxx.h" 
#include "adc.h"
#include "dma.h"
#include "ADScan.h"
#include "AnalogInTrans.h"
#include "DigitalIn.h"
#include "EEPROM.h" 
#include "DigitalOut.h"
#include "IDriver.h"
#include "stm32f1xx.h"
#include "pcf8563.h"
#include "FrmDisplay.h"
#include "Control.h"
#include "Alarm.h"
#include <time.h>
#include "JD_InOut.h"
#include "JD_Lighting.h"
#include "FanControl.h"
#include "DewControl.h"
#include "DefrostControl.h"
#include "JD_Timer.h"
#include "MultipleAlarmClocks.h"

static u8 TaskCount;
static u16 Update;
static u16 TimersUpdate;
static u8 NotCoolFlag;


JDNFSQ Controller;


/*
 * Function Name:   GetControlCommand
 * Description:  ��������
 * Return Value: 
 * Parameter:  
 * Note:
 *Author:yujishuai
*/
u8 GetControlCommand()
{
	u8 value;
	if((GetNotColdStartCondition())&&(NotCoolFlag == 0))
	{
		value = COOL_FAN_OFF;//����
		NotCoolFlag = 1;
		Controller.ManualCommand = value;
		SetParamRegisterValue(HREG_SYS_MODE,COOL_FAN_OFF);//����ģʽ
	}
	else if((!GetNotColdStartCondition())&&(NotCoolFlag >= 1))
	{
		if(NotCoolFlag == 1)
		{
			Controller.ManualCommand = COOL_ON;
			SetParamRegisterValue(HREG_SYS_MODE,COOL_ON);//����ģʽ
		}
		NotCoolFlag = 0;
	}
	else
	{
		if(NotCoolFlag == 1)
		{
			if(Controller.ManualCommand != COOL_FAN_OFF)
			{
				NotCoolFlag = 2;
			}
		}
		value = Controller.ManualCommand;
	}
    return value;
}
void ClearnSlaveFailCount()
{
	Controller.SlaveFailCount = 0;
}


void JD_TimerClale()
{
  u16 TicksNow, TicksElapsed;

  TicksNow = Timer2_Get16Ticks();
  TicksElapsed = TicksNow - TimersUpdate;

  if (TicksElapsed >= APC_TIMERS_DELAY)
  {
    TimersUpdate = Timer2_Get16Ticks();
    JD_Timer();
  }
}
/*
 * Function Name:
 * Description:
 * Return Value:
 * Parameter:
 * Note:
 *Author:
*/
void JD_Control()
{

  switch (Controller.Status)
  {

   case WAIT_STATUS:
        if (0 == Controller.System_Time_Delay)//ѹ������ʱ��
        {
            Controller.Status = COOL_STATUS;					
        }
        break;
   case COOL_STATUS:
   if (0 == Controller.System_Time_Delay)//ѹ������ʱ��
        {
           JD_CoolControl();//����					
        }		
        break;
  case DEFROST_STATUS:

       JD_DefrostControl(); //��˪

    break;
  case DUTY_COOLING_STATUS:

      JD_DutyCycleRun(); //ִ������

    break;
 case CASCADED_STATUS:
  
        JD_CascadedControl();
			
        break;
  default:
    break;
  }
}


void ColdOffControl()
{

SetCoolOff();
SetDewOff();
SetDew12VOff();
SetDEFOff();	
SetDefDIOff();
SetAlarmOff();	
if(Controller.ManualCommand == COOL_OFF_FAN_ON)
{
  SetEvaFanOn();
}
else
{
	SetEvaFanOff();
}
Controller.Defrost.DefrostManual = 0;
Controller.Status = WAIT_STATUS;	
Controller.Ctrl = STOP_CONTROL;
Controller.Defrost.Progress = DEFROST_WAIT;
Controller.Compressor.OutPut = FAULT_STOP;
Controller.Compressor.Status = COMPRESSOR_STATUS_WAIT;
Controller.Compressor.MinStopTimerDelay =TIMER_NOT_USED;
Controller.Compressor.DutyCyclTimerDelay = TIMER_NOT_USED;
Controller.Compressor.MinRunTimerDelay = TIMER_NOT_USED;
Controller.Defrost.DefrostTimerDelay = 0;
Controller.Defrost.DripAlarmDelay = 0;
Controller.WaitDelay = 10;

Controller.Fan.EvaporatorTimerDelay = TIMER_NOT_USED ;	
DefrostTimerIntervalReset();
}


/////Ԥ��  V1.06��ʼ�ѱ����溯���滻
u8  GetNotCoolCmd()
{
	u8 value;

		value = Controller.ManualCommand;
	return value;
}


/*
 * Function Name:   JDKRC_FsmControl
 * Description:  ������ѯ
 * Return Value:
 * Parameter:
 * Note:
 *Author:
*/

void JD_FsmControl()
{
  u16 TicksNow, TicksElapsed;

  TicksNow = Timer2_Get16Ticks();

  TicksElapsed = TicksNow - Update;

  if (TicksElapsed < APC_UPDATE_DELAY)
  {
    return;
  }

  Update = Timer2_Get16Ticks();

	 if(ClearFlag)
	 {
	  	return;
   }
	 if(1 == IGet_SystemTest_Status())
		{
			ISet_AI_Cali_Zero();
			return;
		}   
	
  
    switch (TaskCount)
    {
    case 0:
			if (0 == Controller.WaitDelay)
			{
					if(COOL_FAN_OFF != GetControlCommand())
					{
						JD_Control();

						JD_FaultControl();
					 }
			}
      break;
    case 1:
			if (0 == Controller.WaitDelay1)
			{
					JD_LightingControl(); //����
			}

    case 2:
			if (0 == Controller.WaitDelay)
			{
					if(COOL_FAN_OFF != GetControlCommand())
					{
						JD_FanControl(); //���
					}
			}
      break;
    case 3:
			if (0 == Controller.WaitDelay)
			{
					if(COOL_FAN_OFF != GetControlCommand())
					{			
						JD_DewControl(); //��¶
					}
			}
      break;
    default:
      break;
    }
  
	if( GetControlCommand() >= COOL_FAN_OFF)
	{		
		ColdOffControl();
	}
  TaskCount++;
  if (TaskCount > 3)
  {
    TaskCount = 0;
  }
}

/*
 * Function Name:   JDKRC_FaultControl
 * Description:  ���Ͽ���
 * Return Value:
 * Parameter:
 * Note:
 *Author:
*/
void JD_FaultControl(void)
{

  if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_CONDENSER_HIGH_TEMP].Alarm)
  {
     SetCoolOff();
     Controller.Compressor.Status = COMPRESSOR_STATUS_STOP;
  }
  
  if((GetWarnAndDIValue()&0x1F) != 0){
      SetAlarmOn();
  }else{
      SetAlarmOff();
  }
  
}

/*
 * Function Name:   JDKRC_GetControlStatus
 * Description:  ����������״̬
 * Return Value:
 * Parameter:
 * Note:
 *Author:
*/
SYSTEM_SUATUS JD_GetControlStatus()
{

  if (DEFROST == JD_GetDefrostRequst() || (DEFROST_STATUS == Controller.Status))
  {
    Controller.Status = DEFROST_STATUS;
    Controller.Ctrl = STOP_CONTROL;
  }
  else if (DUTY_CONTROL == GetDutyControl())
  {
    Controller.Status = DUTY_COOLING_STATUS;
    Controller.Ctrl = STOP_CONTROL;
  }
	else if (ON == GetJDParamValue(HREG_CASCADED_MODE_ENABLE))
	{
		if(Controller.SlaveFailCount>=480)
		{
			SetParamRegisterValue(HREG_CASCADED_MODE_ENABLE, 0);
		}
		Controller.Status = CASCADED_STATUS;
		Controller.Ctrl = STOP_CONTROL;
	}
  else
  {
    Controller.Status = COOL_STATUS;
  }

  return Controller.Status;
}
/*
 * Function Name:   JDKRC_Timer_Init
 * Description:  shijian��ʼ��
 * Return Value:
 * Parameter:
 * Note:
 *Author:
*/
u8 JD_Timer_Init()
{
  Controller.System_Time_Delay = GetJDParamValue(HREG_WAIT_TIMER)*3;
  Controller.WaitDelay = 10;
  Controller.AlarmStartDelay = 5;
	Controller.WaitDefaultDelay  = 240;
  Controller.Compressor.MinStopTimerDelay = TIMER_NOT_USED;
  Controller.Compressor.MinRunTimerDelay = TIMER_NOT_USED;
  Controller.Compressor.TowCompStartDelay = TIMER_NOT_USED;
  Controller.Defrost.SyDefrostTimerDelay = TIMER_NOT_USED;
  Controller.Fan.EvaporatorTimerDelay = TIMER_NOT_USED;
  Controller.DoorOpenTime = 0;
  Controller.WaitDelay1 = 10;
	Controller.PwrOnRhAlarmDelay =  60 * GetJDParamValue(HREG_EXTERN_WARN_DELAY);

  return (u8)0;
}
/*
 * Function Name:   JDKRC_Control_Init
 * Description:  ��ʼ��
 * Return Value:
 * Parameter:
 * Note:
 *Author:
*/
u8 JD_Control_Init()
{
  Controller.Status = WAIT_STATUS;
  Controller.Compressor.Status = COMPRESSOR_STATUS_WAIT;
  Controller.Ctrl = STOP_CONTROL;
  Controller.Defrost.Progress = DEFROST_WAIT;
  DefrostTimerIntervalReset();
	if(ClearFlag)
	{
	}
	else
	{
		SetDewOn();
		SetLightOn();
	}

	SetEvaFanOff();
	Controller.LockMode = 1;
  SetParamRegisterValue(HREG_DEFOST_MANUAL, 0);
  Controller.Compressor.OutPut = STOP;
	Controller.ManualCommand = GetJDParamValue(HREG_SYS_MODE);	
	Controller.ManualDew = ON;
	Controller.light.Manual = ON;
	Controller.SlaveFailCount =480;
  //ϵͳ������ʱ����
  JD_Timer_Init();
	
  return (u8)0;
}



/*
 * Function Name:   JDKRC_Application
 * Description:
 * Return Value:
 * Parameter:
 * Note:
 *Author:
*/
u8 JD_Application()
{
  JD_FsmControl();

  JD_TimerClale();

  return (u8)0;
}

/*
 * Function Name:   JD_IconsValueControl
 * Description:     ����״̬�ƿ���ʾ
 * Return Value: 
 * Parameter:  
 * Note:
 *Author:
*/


u16 JD_IconsValueControl(void)
{
	  u8 i = 0;
	  u16 lenvalues = 0;
	
	
     for( i=0;i<12;i++)
	 {
		 switch(i)
		 {
			 case 0:
			 if(IGet_DO_State(DO1_COOL) == 1)
			  lenvalues |= 1<<0;
			   break;
			 case 1:
			if(IGet_DO_State(DO5_DEW) == 1)
			  lenvalues |= 1<<1;
			   break;		 		 
			 case 2:
				 if(IGet_DO_State(DO2_FAN) == 1)
				 lenvalues |= 1<<2;
			   break;
			 case 3:
				 if(Controller.Defrost.Progress == DEF_ON_DELAY)
				 lenvalues |= 1<<3;
				 break;
			 case 4:
				 if(IGet_Warn_States())
				 lenvalues |= 1<<4;
				 break;
			 case 5:
				 if((Controller.Defrost.Progress == DRIP_DELAY)||(Controller.Defrost.Progress == DRIP))
				 lenvalues |= 1<<5;
				 break;
			 case 6:				 
//				 if(IGet_VP_PWM(0))
			  if(IGet_DO_State(DO4_LED) == 1)
				 lenvalues |= 1<<6;
				 break;
			 case 7:
				 if(GetDoorDi())
				 lenvalues |= 1<<7;
				 break;
			  case 8:
				 if((DEF_MAINFORM != ActiveForm)&&(DEF_TRIP_FORM != ActiveForm))
				 lenvalues |= 1<<8;
				 break;	 
				  case 9:					
				 if((2 == Controller.light.Manual)&&(0==GetNSBStatus()))
				  lenvalues |= 1<<9;
				 break;	 
				  case 10:
					 if( GetNSBStatus())
				 lenvalues |= 1<<10;
				 break;	 
					case 11:			
				 if((GetJDParamValue(HREG_BZ_SET)==1)&&(IGet_Warn_States()))
				 lenvalues |= 1<<11;
				 break;	 			 
		 }		 
	 } 
   return lenvalues;
}




	
